<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="#top">top</a>
<a href="#left">left</a>
<a href="#right">right</a>
<hr/>
<span id="s1"></span>
</body>
</html>
<script>
    // 当hash值发生改变时执行
    onhashchange = function () {
        let hash = location.hash
        if (hash === '#top') {
            document.getElementById('s1').innerText = 'top'
        } else if (hash === '#left') {
            document.getElementById('s1').innerText = 'left'
        } else {
            document.getElementById('s1').innerText = 'right'
        }
    }
</script>